<?php

namespace App\Console\Commands;

use App\Models\Deposit;
use App\Models\User;
use App\Models\Withdraw;
use Illuminate\Console\Command;

class ExportData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'export:data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->export_deposit();
        $this->export_withdraw();

        return Command::SUCCESS;
    }

    public function export_deposit()
    {
        $data = [];
        $deposits = Deposit::get();
        $deposits->load('user');

        foreach($deposits as $d) {
            $data[] = [
                'user_id' => $d->user_id,
                'email' => $d->user->email ?? '',
                'phone' => $d->user->phone ?? '',
                'asset' => $d->coin,
                'amount' => $d->amount,
                'from_address' => $d->from_address,
                'to_address' => $d->to_address,
                'txid' => $d->txid,
                'status' => $d->status,
                'created_at' => $d->created_at,
            ];
        }

        file_put_contents(storage_path("deposit.json"), json_encode($data));
    }

    public function export_withdraw()
    {
        $data = [];
        $withdraws = Withdraw::get();
        $withdraws->load('user');

        foreach($withdraws as $d) {
            $data[] = [
                'email' => $d->user->email ?? '',
                'phone' => $d->user->phone ?? '',
                'user_id' => $d->user_id,
                'asset' => $d->coin,
                'amount' => $d->amount,
                'fee' => $d->fee,
                'to_address' => $d->address,
                'txid' => $d->txid,
                'status' => $d->status,
                'created_at' => $d->created_at,
            ];
        }

        file_put_contents(storage_path("withdraw.json"), json_encode($data));

    }
}
